//
//  TravelNotesController.m
//  TravelNotes
//
//  Created by qianfeng on 15-7-7.
//  Copyright (c) 2015年 ZWB. All rights reserved.
//

#import "TravelNotesController.h"
#import "AFNetworking.h"
#import "TravelModel.h"
#import "TravelCell.h"
#import "UserModel.h"

@interface TravelNotesController () <UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    AFHTTPRequestOperationManager *_manager;
}

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation TravelNotesController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor  = [UIColor clearColor];
    self.collectionView.backgroundColor=[UIColor whiteColor];

    
    [self initUI];
    [self firstDownload];

}

- (void)initUI {
    self.dataArray = [[NSMutableArray alloc] init];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(kScreenSize.width, 220);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 5;
    flowLayout.sectionInset = UIEdgeInsetsMake(2, 2, 2, 2);
//    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];

    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height-100) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"TravelCell" bundle:nil] forCellWithReuseIdentifier:@"TravelCell"];
    
    _manager = [AFHTTPRequestOperationManager manager];
    _manager.responseSerializer = [AFHTTPResponseSerializer serializer];
}

- (void)firstDownload {
    self.currentPage = 1;

    NSString *url = [NSString stringWithFormat:kTravelUrl, self.currentPage];
    [self addTaskWithUrl:url isRefresh:NO];
}

- (void)addTaskWithUrl:(NSString *)url isRefresh:(BOOL)isRefresh {
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"开始下载");
        if (responseObject) {
            NSArray *array = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            for (NSDictionary *dict in array) {
                TravelModel *travelModel = [[TravelModel alloc] init];
                [travelModel setValuesForKeysWithDictionary:dict];

                NSDictionary *dict2 = dict[@"user"];
                travelModel.image = dict2[@"image"];
                
                [self.dataArray addObject:travelModel];
            }
            
            [self.collectionView reloadData];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载失败");
    }];
}

#pragma mark - collectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    TravelCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TravelCell" forIndexPath:indexPath];
    TravelModel *model = self.dataArray[indexPath.row];
    [cell showDataWithModel:model];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

@end
